package model;

import database.Database;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class Person {
	
	/*
	 * REGION private fields
	 */
	private int ID;
	private String firstName;
	private String lastName;
	private String username;
	private String password;
	private String email;
	private int phoneNumber;
	private Date dateOfBirth;
	
	/*
	 * REGION public constructors
	 */
	public Person(int ID) {
		setID(ID);
		
		load();
	}
	
	public Person(String firstName, String lastName, String username, String password, String email, int phoneNumber, Date dateOfBirth) {
		setFirstName(firstName);
		setLastName(lastName);
		setUsername(username);
		setPassword(password);
		setEmail(email);
		setPhoneNumber(phoneNumber);
		setDateOfBirth(dateOfBirth);
	}
	
	/*
	 * REGION public getters
	 */
	public int getID() {
		return ID;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public String getUsername() {
		return username;
	}
	
	public String getPassword() {
		return password;
	}

	public String getEmail() {
		return email;
	}
	
	public int getPhoneNumber() {
		return phoneNumber;
	}
	
	public Date getDateOfBirth() {
		return dateOfBirth;
	}
	
	/*
	 * REGION public setters
	 */	
	public void setFirstName(String firstName) {
		if (firstName.length() < 50) {
			this.firstName = firstName;
		}
	}
	
	public void setLastName(String lastName) {
		if (lastName.length() < 50) {
			this.lastName = lastName;
		}
	}
	
	public void setUsername(String username) {
		if (username.length() < 50) {
			this.username = username;
		}
	}
	
	public void setPassword(String password) {
		if (password.length() < 50) {
			this.password = password;
		}
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	public void setPhoneNumber(int phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	
	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}
	
	/*
	 * REGION private setters
	 */
	private void setID(int ID) {
		this.ID = ID;
	}
	
	/*
	 * REGION public function
	 */
	public void load() {
		
		// Establish a database connection
		Database database = new Database();
		database.connectToDatabase();
		
		try {
			ResultSet rs = database.getUser(getID());
			
			if (rs.next()) {
				
				setFirstName(rs.getString("firstName"));
				setLastName(rs.getString("lastName"));
				setUsername(rs.getString("username"));
				setPassword(rs.getString("password"));
				setEmail(rs.getString("email"));
				setPhoneNumber(rs.getInt("phoneNumber"));
				setDateOfBirth(rs.getDate("dateOfBirth"));
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		database.disconnectDatabase();
	}
	
	public void save() {
		
		// Establish a connection to the database
		Database database = new Database();
		database.connectToDatabase();
		
		if (ID == 0) {
			database.addUser(firstName, lastName, username, password, email, phoneNumber, dateOfBirth.toString());
			ResultSet rs = database.userGetIDFromFirstAndLastName(firstName, lastName);
			try{
				if (rs.next()){
					setID(rs.getInt("ID"));
				}
				else{
					System.out.println("IDoppdatering gikk til helvete");
				}
			}catch(SQLException e){
					e.printStackTrace();
			}
					
		}
		
		
		else {
			database.updateUser(ID, firstName, lastName, username, password, email, phoneNumber, dateOfBirth.toString());
		}
		database.disconnectDatabase();
	}
		
	/*
	 * REGION override methods
	 */
	public boolean equals(Object obj) {
		if (super.equals(obj))
			return true;
		
		if (obj.getClass() != this.getClass())
			return false;
		
		Person aPerson = (Person)obj;
		
		if (aPerson.getFirstName().compareTo(getFirstName()) != 0) 
			return false;
		if (aPerson.getEmail().compareTo(getEmail()) != 0)
			return false;
	
		return true;
	}
	
	public String toString() {
		return getID() + ";" + getFirstName() + ";" + getLastName() + ";" + getUsername() + ";" + getPassword() + ";" + getEmail() + ";" + getPhoneNumber() + ";" + getDateOfBirth().toString();
	}
	public int getNumberOfInvites(){
		Database db = new Database();
		db.connectToDatabase();
		int antall = 0;
		try{
			ResultSet rs = db.getNumberOfInvites(this.ID);
			if(rs.next()){
				antall = rs.getInt("tall");
			}
			System.out.println("fant, eller fant ikke noe");
		}catch(SQLException e){
			e.printStackTrace();
		}
		return antall;
	
	}
	public void printAlleInv(){
		Database db = new Database();
		db.connectToDatabase();
		try{
			ResultSet rs = db.getInvitationsForUser(this.ID);
			while(rs.next()){
				System.out.println(""+rs.getInt("ID")+";"+rs.getInt("userId")+";"+rs.getInt("MeetingId"));
			}
			System.out.println("fant, eller fant ikke noe");
		}catch(SQLException e){
			e.printStackTrace();
		}
		
	}
	public ArrayList<Invitation> invitations(){
		Database db = new Database();
		db.connectToDatabase();
		ArrayList<Invitation> invs = new ArrayList<Invitation>();
		try{
			ResultSet rs = db.getInvitationsForUser(this.ID);
			while(rs.next()){
				invs.add(new Invitation(rs.getInt("id")));
			}
			
		}catch(SQLException e){
			e.printStackTrace();
		}
		return invs;
	}
	public ArrayList<Invitation> pendingInvitations(){
		Database db = new Database();
		db.connectToDatabase();
		ArrayList<Invitation> invs = new ArrayList<Invitation>();
		try{
			ResultSet rs = db.getPendingInvitationsForUser(this.ID);
			while(rs.next()){
				invs.add(new Invitation(rs.getInt("id")));
			}
			System.out.println("fant, eller fant ikke noe");
		}catch(SQLException e){
			e.printStackTrace();
		}
		return invs;
	}
}
